﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单击单元格背景变色</title>
    <style>
        .clickTD{
            background-color:#ff0000;
            text-decoration:none;}
    </style>
    <script type="text/javascript">
        function colorChange(t)
        {
            for (var i=0; i<t.rows.length; i++)   //遍历行
                for (var j=0; j<t.rows[i].cells.length; j++)//遍历列
                    t.rows[i].cells[j].className = t.rows[i].cells[j] == event.srcElement ? 'clickTD' : '' //改变背景色
        }
    </script>
</head>
<body>
<table border="1" onClick="colorChange(this)">
    <tr>
        <td>第一行</td><td>第一行第二列</td>
    </tr>
    <tr>
        <td>第二行</td><td>第二行第二列</td>
    </tr>
</table>
</body>
</html>